package cn.jaye.fabric.controller;

import cn.jaye.fabric.application.ChainCodeApplicationService;
import cn.jaye.fabric.application.command.chaincode.QueryChainCodeCommand;
import cn.jaye.fabric.common.constants.FarbicConstant;
import cn.jaye.fabric.common.constants.Global;
import cn.jaye.fabric.common.constants.GlobalConstant;
import cn.jaye.fabric.common.util.ResultUtil;
import cn.jaye.fabric.domain.model.User;
import cn.jaye.fabric.domain.repository.PeerRepository;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2018/5/7.
 */
@RestController
@RequestMapping("test")
public class TestController {


    @Autowired
    private ChainCodeApplicationService chainCodeApplicationService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private FarbicConstant farbicConstant;

    @Autowired
    private GlobalConstant globalConstant;

    @Autowired
    private PeerRepository peerRepository;

    @PostMapping("peer/get")
    public String peer() {
        return null;
    }

    @PostMapping("redis/get")
    public String redis() {
        redisTemplate.opsForValue().set("foo", "bar");
        return JSON.toJSONString(redisTemplate.opsForValue().get("foo"));
    }

    @PostMapping("/test")
    public String test(HttpServletRequest request, @RequestBody QueryChainCodeCommand queryChainCodeCommand) throws Exception {
        return ResultUtil.success(chainCodeApplicationService.test(queryChainCodeCommand));
    }

    //    @PostMapping("/send")
//    public String send() {
//        template.send("topic", "testKey", "test");
//        return "success";
//    }
    @PostMapping("/shell")
    public String shell(HttpServletRequest request, @RequestBody QueryChainCodeCommand queryChainCodeCommand) throws Exception {

        return ResultUtil.success(null);
    }
    @PostMapping("/peer/channlName")
    public String peer(HttpServletRequest request) throws Exception {
        return ResultUtil.success(peerRepository.selectPeerByChannel("channel1"));
    }
}
